package com.chinameyer.workorders.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinameyer.workorders.bean.MeyerCatalogue;
import com.chinameyer.workorders.dto.catalogue.AddCatalogueDTO;
import com.chinameyer.workorders.dto.catalogue.EditCatalogueDTO;
import com.chinameyer.workorders.mapper.CatalogueMapper;
import com.chinameyer.workorders.vo.CatalogueVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author meyer@HongYe
 */
@Service
public class CatalogueService {

    @Resource
    private CatalogueMapper catalogueMapper;


    public boolean checkCatalogueIsExist(AddCatalogueDTO addCatalogueDTO) {
        QueryWrapper<MeyerCatalogue> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerCatalogue::getCatalogueName,addCatalogueDTO.getCatalogueName())
                .eq(MeyerCatalogue::getPid,addCatalogueDTO.getPid());
        return this.catalogueMapper.selectCount(wrapper) > 0;
    }

    @Transactional
    public int addCatalogue(AddCatalogueDTO addCatalogueDTO) {
        MeyerCatalogue catalogue = new MeyerCatalogue();
        BeanUtils.copyProperties(addCatalogueDTO,catalogue);
        int level = this.getLevel(addCatalogueDTO.getPid());
        catalogue.setLevel(level);
        this.catalogueMapper.insert(catalogue);
        return catalogue.getId();
    }

    public int getLevel(int cataloguePID) {
        if (cataloguePID == 0) {
            return 1;
        }
        int pid = this.catalogueMapper.selectById(cataloguePID).getPid();
        if (pid == 0) {
            return 2;
        }
        return 3;
    }

    @Transactional
    public void deleteCatalogue(Integer catalogueId) {
        QueryWrapper<MeyerCatalogue> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerCatalogue::getId,catalogueId);
        this.catalogueMapper.delete(wrapper);
    }

    public boolean checkCatalogueIsExistExpectItself(EditCatalogueDTO editCatalogueDTO) {
        QueryWrapper<MeyerCatalogue> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerCatalogue::getCatalogueName,editCatalogueDTO.getCatalogueName())
                .eq(MeyerCatalogue::getPid,editCatalogueDTO.getPid())
                .ne(MeyerCatalogue::getId,editCatalogueDTO.getId());
        return this.catalogueMapper.selectCount(wrapper) > 0;
    }

    @Transactional
    public void updateCatalogue(EditCatalogueDTO editCatalogueDTO) {
        MeyerCatalogue catalogue = new MeyerCatalogue();
        BeanUtils.copyProperties(editCatalogueDTO,catalogue);
        int level = this.getLevel(editCatalogueDTO.getPid());
        catalogue.setLevel(level);
        this.catalogueMapper.updateById(catalogue);
    }

    public List<Tree<String>> getAllCatalogue() {
        List<CatalogueVO> catalogueVOList = this.catalogueMapper.getAllCatalogue();
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        catalogueVOList.forEach(catalog -> {
            nodeList.add(new TreeNode<>(catalog.getId()+"",catalog.getPid()+"",catalog.getCatalogueName(),catalog.getLevel()));
        });
        return TreeUtil.build(nodeList, "0");
    }
}
